package me.ingxin.android.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.Outline
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatDialog
import androidx.core.view.setPadding
import me.ingxin.android.tools.dp2px
import me.ingxin.android.view.LoadingView
import kotlin.math.max
import kotlin.math.min

/**
 * Created by ingxin on 2022/11/26
 * 菊花loading对话框
 */
class LoadingDialog(context: Context) : AppCompatDialog(context) {

    private var tipWord: CharSequence? = null
    private var dimAmount = 0f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(createDialogView())
        setCanceledOnTouchOutside(false)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setDimAmount(dimAmount)
    }

    /**
     * 设置提示文本
     * @param tipWord 需要提示的文本,null时不提示
     */
    fun setTipWord(tipWord: CharSequence?): LoadingDialog {
        this.tipWord = tipWord
        return this
    }

    /**
     * 设置背景昏暗度
     * @param amount 取值范围在0到1
     */
    fun setDimAmount(amount: Float): LoadingDialog {
        this.dimAmount = max(0f, min(1f, amount))
        return this
    }

    private fun createDialogView(): View {
        val dialogView = LinearLayout(context).apply {
            orientation = LinearLayout.VERTICAL
            gravity = Gravity.CENTER
            val padding = context.dp2px(14f)
            this.background = ColorDrawable(0xC0000000.toInt())
            setPadding(padding)
            minimumWidth = context.dp2px(120f)
            minimumHeight = context.dp2px(80f)
            //设置轮廓
            outlineProvider = object : ViewOutlineProvider() {
                override fun getOutline(view: View?, outline: Outline?) {
                    view ?: return
                    val radius = context.dp2px(15f).toFloat()
                    //设置轮廓范围
                    outline?.setRoundRect(0, 0, view.width, view.height, radius)
                    //裁剪轮廓外的部分
                    view.clipToOutline = true
                }
            }
        }

        val loadingView = LoadingView(context).apply {
            setColor(Color.WHITE)
            setDuration(600)
        }
        val layoutParams = LinearLayout.LayoutParams(
            context.dp2px(32f),context.dp2px(32f)
        )
        dialogView.addView(loadingView, layoutParams)

        if (tipWord?.isNotEmpty() == true) {
            val tipView = TextView(context).apply {
                ellipsize = TextUtils.TruncateAt.END
                gravity = Gravity.CENTER
                setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f)
                setTextColor(Color.WHITE)
                text = tipWord
            }

            val lp = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
            )
            lp.topMargin = context.dp2px(12f)
            dialogView.addView(tipView, lp)
        }

        return dialogView
    }

}